package cn.o.elastic.service.impl;

import cn.o.elastic.repo.UserRepository;
import cn.o.elastic.service.UserService;
import cn.o.entity.system.user.User;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author: Zhou Chenyu
 * @Date: 2018/7/6
 * @Version: V1.0
 * @Modified By:
 * @Modified Date:
 * @Why:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public void create(User user) {
        userRepository.save(user);
    }

    @Override
    public void delete(User user) {
        userRepository.delete(user);
    }

    @Override
    public List<User> list(String keyword) {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(keyword);
        Iterable<User> searchResult = userRepository.search(builder);
        List<User> list = new ArrayList<>();
        for (Iterator iter = searchResult.iterator(); iter.hasNext();) {
            list.add((User) iter.next());
        }
        return list;
    }
}
